
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.core.mvc.CHActor;

/** 进度条
 * 
 * @author haocao */
public class CHProgressBar extends Group {

	private float progress = 0f;

	public CHProgressBar () {
	}

	public CHProgressBar (String bgPath, String lightPath) {
		this(Chao.game.getTexture(bgPath), Chao.game.getTexture(lightPath));
	}

	private CHActor bgActor, lightActor;

	public CHProgressBar (Texture bgTexture, Texture lightTexture) {
		setTexture(bgTexture, lightTexture);
	}

	public void setTexture (Texture bgTexture, Texture lightTexture) {
		bgActor = CHActor.obtain();
		bgActor.setBgTexture(bgTexture);

		lightActor = CHActor.obtain();
		lightActor.setBgTexture(lightTexture);

		setSize(bgActor.getWidth(), bgActor.getHeight());

		addActor(lightActor);
	}

	@Override
	public void act (float delta) {
		// TODO Auto-generated method stub
		super.act(delta);
	}

	/** 进度（0~1）
	 * @return */
	public float getProgress () {
		return progress;
	}

	public void setProgress (float progress) {
		this.progress = progress;
		if (progress > 1) {
			progress = 1;
		}
	}


	private Rectangle scissors = new Rectangle();
	@Override
	public void draw (Batch batch, float parentAlpha) {

		bgActor.setPosition(getX(), getY());
		bgActor.draw(batch, parentAlpha);
		batch.end();
		batch.begin();

		// 在可选区域显示纹理
		Rectangle clipBounds = new Rectangle(getX(), getY(), getWidth() * Math.max(progress, 0.01f), getHeight());
		ScissorStack.calculateScissors(getStage().getCamera(), batch.getTransformMatrix(), clipBounds, scissors);
		ScissorStack.pushScissors(scissors);
		super.draw(batch, parentAlpha);
		batch.flush();
		ScissorStack.popScissors();
	}

	public void dispose () {
		bgActor.getBgTexture().dispose();
		lightActor.getBgTexture().dispose();
	}
}
